﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Divelements.SandDock;
using Hydra.Shell.Interfaces;
using Hydra.Shell.Enumerations;
using System.Collections.Generic;
using System.Linq;

namespace Hydra.Shell.Implementations
{
    public class HydraDocumentWindow : DocumentWindow, IHydraWindowFrame, IObjectWithSite
    {
        public IUnknown Document { get; set; }
        private IUnknown site;
        private IHydraUIInternals hydraUIInternals;
        private Dictionary<HydraFrameProperties, object> properties; 

        public HydraDocumentWindow()
        {
            properties = new Dictionary<HydraFrameProperties, object>();

            this.Closing += new EventHandler<CancelEventArgs>(HydraDocumentWindow_Closing);
        }

        private void HydraDocumentWindow_Closing(object sender, CancelEventArgs e)
        {
        //    IPersist 

        //    CloseFrame(FrameClose.PromptSave);
        }

        public bool CloseFrame(FrameClose grfSaveOptions)
        {
            switch (grfSaveOptions)
            {
                case FrameClose.None:
                    break;
                case FrameClose.PromptSave:
                    break;
                case FrameClose.SaveIfDirty:
                    break;
                case FrameClose.NoSave:
                    break;
            }

            return true;
        }

        public bool GetFramePos(HydraSetFramePos[] pdwSFP, out Guid pguidRelativeTo, out int px, out int py, out int pcx, out int pcy)
        {
            throw new NotImplementedException();
        }

        public bool GetGuidProperty(HydraFrameProperties propid, out Guid pguid)
        {
            throw new NotImplementedException();
        }

        public bool GetProperty(HydraFrameProperties propid, out object pvar)
        {
            if (properties.ContainsKey(propid))
            {
                pvar = properties[propid];
                return true;
            }
            else
            {
                pvar = null;
                return false;
            }
        }

        public new bool Hide()
        {
            throw new NotImplementedException();
        }

        public bool IsOnScreen
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsVisible
        {
            get { throw new NotImplementedException(); }
        }

        public bool QueryViewInterface(ref Guid riid, out IUnknown ppv)
        {
            throw new NotImplementedException();
        }

        public bool SetFramePos(HydraSetFramePos dwSFP, ref Guid rguidRelativeTo, int x, int y, int cx, int cy)
        {
            throw new NotImplementedException();
        }

        public bool SetGuidProperty(HydraFrameProperties propid, ref Guid rguid)
        {
            throw new NotImplementedException();
        }

        public bool SetProperty(HydraFrameProperties propid, object var)
        {
            if (properties.ContainsKey(propid))
            {
                properties[propid] = var;
            }
            else
            {
                properties.Add(propid, var);
            }

            switch (propid)
            {
                case HydraFrameProperties.DocView:
                    this.Content = var;
                    break;
                case HydraFrameProperties.DocData:
                    this.Document = (IUnknown)var;
                    break;
                case HydraFrameProperties.EditorCaption:
                    this.Title = (string)var;
                    break;
            }

            return true;
        }

        public bool ShowNoActivate()
        {
            var documentContainer = hydraUIInternals.DocumentContainer;
            HydraWindowGroup windowGroup;

            if (documentContainer.Items.OfType<HydraWindowGroup>().Count() > 0)
            {
                windowGroup = hydraUIInternals.DocumentContainer.Items.OfType<HydraWindowGroup>().First();
            }
            else
            {
                windowGroup = new HydraWindowGroup();

                documentContainer.Items.Add(windowGroup);
            }

            windowGroup.Items.Add(this);

            return true;
        }

        public bool Show()
        {
            var documentContainer = hydraUIInternals.DocumentContainer;
            HydraWindowGroup windowGroup;

            if (documentContainer.Items.OfType<HydraWindowGroup>().Count() > 0)
            {
                windowGroup = hydraUIInternals.DocumentContainer.Items.OfType<HydraWindowGroup>().First();
            }
            else
            {
                windowGroup = new HydraWindowGroup();

                documentContainer.Items.Add(windowGroup);
            }

            windowGroup.Items.Insert(0, this);
            windowGroup.SelectedWindow = this;

            return true;
        }

        public bool Advise(IHydraWindowFrameNotify pNotify, out uint pdwCookie)
        {
            throw new NotImplementedException();
        }

        public bool Unadvise(uint dwCookie)
        {
            throw new NotImplementedException();
        }

        public bool ActivateOwnerDockedWindow()
        {
            throw new NotImplementedException();
        }

        public bool IsDisposed
        {
            get { throw new NotImplementedException(); }
        }

        public bool QueryInterface(Guid iid, out IUnknown interfaceObject)
        {
            throw new NotImplementedException();
        }

        public bool QueryInterface<T>(out T interfaceObject)
        {
            throw new NotImplementedException();
        }

        public void SetSite(IUnknown pUnkSite)
        {
            site = pUnkSite;

            site.QueryInterface<IHydraUIInternals>(out hydraUIInternals);
        }

        public bool GetSite(Guid riid, out IUnknown ppvSite)
        {
            throw new NotImplementedException();
        }

        public bool GetSite<T>(Guid riid, out T ppvSite)
        {
            throw new NotImplementedException();
        }
    }
}
